package com.rdgzs.framework.web;


import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.opensymphony.xwork2.ActionSupport;
import com.rdgzs.framework.constant.SysConstants;
import com.rdgzs.framework.utilsTools.UtilsObject;

@Namespace("/exception")
public class AjaxExceptionHandleAction extends ActionSupport {

	private static final long serialVersionUID = 668305397469726147L;
	private Logger logger = LoggerFactory.getLogger(getClass());
	@Override
	public String execute() throws Exception{
		String isSuccess = "0";	
		String exceptionMessage= SysConstants.exceptionMessage;
		//Exception ex = (Exception)ServletActionContext.getRequest().getAttribute("javax.servlet.error.exception"); 
		Exception ex2 = (Exception)ServletActionContext.getRequest().getAttribute("exception");
		if(null !=ex2){
			exceptionMessage = ex2.getMessage();
			//Exception innerException = (Exception) ex2.getCause();
			//ToDo 把异常记录到日志里。需要优化。
			logger.error(ex2.getMessage(), ex2); 
		}
		Map map = new HashMap();
		map.put("isSuccess", isSuccess);
		map.put("exceptionMessage", exceptionMessage);
		
		JSONObject jsonObject = JSONObject.fromObject(map);
		UtilsObject.WriteAjaxInfoJson(ServletActionContext.getResponse(), jsonObject.toString());
		return null;
	}
}
